#include "../include/Response.hpp"

struct Impl_Response{
    std::string version;  // 协议版本
    int status;  // 状态码
    std::string describe; // 描述
    std::map<std::string,std::string> msg;  // 头部字段信息
    std::string text;  // 正文
};

const std::map<std::string,std::string>& ResponseHead::getMsg() const {
    return pimpl->msg;
}

ResponseHead::ResponseHead(){
    pimpl=std::shared_ptr<Impl_Response>(new(std::nothrow) Impl_Response());
}

Response::Response(std::shared_ptr<HttpRequest> head,int send):_send(send){
    pesHead.setVersion(head->getProtocol()+"/"+ head->getVersion());
    pesHead.setMsg("Content-Type","text/html");
}
Response::Response(std::shared_ptr<HttpRequest> head):_send(0),_close(1){
    pesHead.setVersion(head->getProtocol()+"/"+ head->getVersion());
    pesHead.setMsg("Content-Type","text/html");
}
void Response::setSend(int send){
    _send=send;
}

int Response::getSend(){
    return _send;
}

Response::Response(std::shared_ptr<RequestHead> head){
    pesHead.setVersion_self(head->getVersion());
    // pesHead.setMsg("Content-Type","text/html");
}

void ResponseHead::setStatus(int status){
    this->pimpl->status=status;
}
void ResponseHead::setText(std::string text){
    this->pimpl->text=text;
}

void ResponseHead::setDescribe(std::string describe){
    this->pimpl->describe=describe;
}


void ResponseHead::setVersion_self(const std::string& version){
    this->pimpl->version=version;
}

void ResponseHead::setVersion(std::string version){
    this->pimpl->version=version;
}


void ResponseHead::setMsg(std::string key,std::string value){
    // this->pimpl->msg.insert(std::pair<std::string,std::string>(key,value));
    this->pimpl->msg[key]=value;
}

void ResponseHead::clear(){
    this->pimpl->msg.clear();
}

std::string Response::returnResponse(){
    std::string s=pesHead.pimpl->version;
    s.append(" ");
    s.append(std::to_string(pesHead.pimpl->status));
    s.append(" ");
    s.append(pesHead.pimpl->describe);
    s.append("\r\n");
    for(auto t=pesHead.pimpl->msg.begin();t!=pesHead.pimpl->msg.end();t++){
        s.append(t->first);
        s.append(":");
        s.append(t->second);
        s.append("\r\n");
    }
    s.append("\r\n");
    s.append(pesHead.pimpl->text);
    size=s.size();
    return s;
}

void Response::setStatus(int status){
    pesHead.setStatus(status);
}
void Response::setText(std::string text){
    // std::cout<<text<<std::endl;
    // std::cout<<text.size()<<std::endl;
    pesHead.setText(text);
    pesHead.setMsg("Content-Length",std::to_string(text.size()));
}

void Response::setClose(int close){
    _close=close;
}

int Response::getClose(){
    return _close;
}

void Response::setDescribe(std::string describe){
    pesHead.setDescribe(describe);
}


void Response::setVersion_self(const std::string& version){
    pesHead.setVersion(version);
}
void Response::setVersion(std::string version){
    pesHead.setVersion(version);
}

void Response::setMsg(std::string key,std::string value){
    pesHead.setMsg(key,value);
}

void Response::clear(){
    pesHead.clear();
}
